﻿using System;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Menu;
using System.Collections.Generic;
using System.Drawing;
using eFatura.Properties;

namespace eFatura.Utility.Grid
{
    public class GridMenu
    {
        #region Members
        private GridManager gridManager;

        private GridViewMenu customMenu;
        private List<EventHandler> customEvents = new List<EventHandler>();

        private GridViewMenu customColumnMenu;
        private List<EventHandler> customColumnEvents = new List<EventHandler>();


        private DXSubMenuItem miDuzenle = new DXSubMenuItem("Edit");
        private bool isReadOnly = false;

        public GridViewMenu RowMenu;

        public GridViewMenu ColumnMenu;

        public GridManager GridManager
        {
            get { return gridManager; }
            set { gridManager = value; }
        }

        public GridView GridView
        {
            get { return GridManager.GridView; }
        }

        public GridUtility Utility
        {
            get { return GridManager.gridUtility; }

        }

        public bool IsReadOnly
        {
            get
            {
                return isReadOnly;
            }
            set
            {
                isReadOnly = value;
                CreateRowMenu();
            }
        }
        #endregion

        #region Constructor
        public GridMenu(GridManager gridManager)
        {
            GridManager = gridManager;

            customMenu = new GridViewMenu(this.GridView);
            customColumnMenu = new GridViewMenu(this.GridView);
            miDuzenle = new DXSubMenuItem("Edit");

            CreateRowMenu();
            CreateColumnMenu();
        }
        #endregion

        private string currentColumnName = "";
        public string CurrentColumnName
        {
            get { return currentColumnName; }
            set
            {
                currentColumnName = value;
                CreateColumnMenu();
            }
        }

        private string currentBandName = "";
        public string CurrentBandName
        {
            get { return currentBandName; }
            set
            {
                currentBandName = value;
                CreateColumnMenu();
            }
        }

        public void CreateColumnMenu()
        {
            ColumnMenu = new GridViewMenu(this.GridView);
            if (CurrentColumnName != "")
            {
                DXSubMenuItem holder = new DXSubMenuItem("Freeze");
                holder.BeginGroup = true;
                DXMenuCheckItem chk = new DXMenuCheckItem();
                chk.Checked = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.Caption = "This column";
                chk.CheckedChanged += chkFreeze_CheckedChanged;
                holder.Items.Add(chk);

                chk = new DXMenuCheckItem();
                chk.Caption = "This column and left ones";
                chk.Checked = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.CheckedChanged += chk_FreezeLeft;

                holder.Items.Add(chk);
                ColumnMenu.Items.Add(holder);
            }
            else if (CurrentBandName != "")
            {
                DXMenuCheckItem chk = new DXMenuCheckItem();
                chk.BeginGroup = true;
                DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bgv = (DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)GridView;
                chk.Checked = (bgv.Bands[CurrentBandName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.Caption = "Freeze this band";
                //chk.Caption = CurrentColumnName + ":" + chk.Checked.ToString();
                chk.CheckedChanged += chkBandiDondur_CheckedChanged;
                ColumnMenu.Items.Add(chk);
            }

            DXMenuItem menuItemMRG = new DXMenuItem("Cell Merge/Unmerge", new EventHandler(AllowCellMerge_Click), Resources.miGerce);
            ColumnMenu.Items.Add(menuItemMRG);
             
           

            //optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Show Grouped Columns", GridView.OptionsView.ShowGroupedColumns, null, new EventHandler(ShowGroupedColumns_Click)));
        }

        void chkBandiDondur_CheckedChanged(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bgv = (DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)GridView;
            bool isfixed = (bgv.Bands[CurrentBandName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);

            if (isfixed)
                bgv.Bands[CurrentBandName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
            else
                bgv.Bands[CurrentBandName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            CreateColumnMenu();
        }

        void chk_FreezeLeft(object sender, EventArgs e)
        {
            int visibleIndex = GridView.Columns[CurrentColumnName].VisibleIndex;
            bool isfixed = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
            List<DevExpress.XtraGrid.Columns.GridColumn> cols = new List<DevExpress.XtraGrid.Columns.GridColumn>();
            for (int i = 0; i < visibleIndex + 1; i++)
            {
                cols.Add(GridView.VisibleColumns[i]);
            }
            foreach (var o in cols)
            {
                if (isfixed)
                    o.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                else
                    o.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            }

            CreateColumnMenu();
        }

        void chkFreeze_CheckedChanged(object sender, EventArgs e)
        {
            bool isfixed = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
            if (isfixed)
                GridView.Columns[CurrentColumnName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
            else
                GridView.Columns[CurrentColumnName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            CreateColumnMenu();
        }

        public void CreateRowMenu()
        {
            RowMenu = new GridViewMenu(this.GridView);
            miDuzenle = new DXSubMenuItem("Edit");
            if (customMenu.Items.Count > 0)
            {
                for (int i = 0; i < customMenu.Items.Count; i++)
                {
                    DXMenuItem mi = customMenu.Items[i];
                    mi.Click -= customEvents[i];
                    mi.Click += customEvents[i];
                    RowMenu.Items.Add(mi);
                }

                int index = RowMenu.Items.Add(miDuzenle);
                RowMenu.Items[index].BeginGroup = true;
            }
            AddStandartMenuItems();
        }

        public void AddMenu(string caption, EventHandler click)
        {
            DXMenuItem di = new DXMenuItem(caption);
            customEvents.Add(click);
            customMenu.Items.Add(di);

            CreateRowMenu();
        }

        public void AddColumnMenu(string caption, EventHandler click)
        {
            DXMenuItem di = new DXMenuItem(caption);
            customColumnEvents.Add(click);
            customColumnMenu.Items.Add(di);

            CreateColumnMenu();
        }

        public void AddColumnMenu(string caption, string Base, EventHandler click)
        {
            DXMenuItem di = new DXMenuItem(caption);
            customColumnEvents.Add(click);
            customColumnMenu.Items.Add(di);

            CreateColumnMenu();
        }

        public void AddMenu(string caption, EventHandler click, Image image)
        {
            DXMenuItem di = new DXMenuItem(caption);
            di.Image = image;
            customEvents.Add(click);
            customMenu.Items.Add(di);

            CreateRowMenu();
        }

        public void AddMenu(string caption, EventHandler click, Image image, bool beginGroup)
        {
            DXMenuItem di = new DXMenuItem(caption);
            di.Image = image;
            customEvents.Add(click);
            customMenu.Items.Add(di);
            di.BeginGroup = beginGroup;
            CreateRowMenu();
        }

        public void RemoveColumnMenu(string caption)
        {
            for (int i = 0; i < customColumnMenu.Items.Count; i++)
            {
                DXMenuItem mi = customColumnMenu.Items[i];
                if (mi.Caption == caption)
                {
                    customColumnMenu.Items.RemoveAt(i);
                    customColumnEvents.RemoveAt(i);
                    CreateColumnMenu();
                    break;
                }
            }
        }

        public void RemoveMenu(string caption)
        {
            for (int i = 0; i < customMenu.Items.Count; i++)
            {
                DXMenuItem mi = customMenu.Items[i];
                if (mi.Caption == caption)
                {
                    customMenu.Items.RemoveAt(i);
                    customEvents.RemoveAt(i);
                    CreateRowMenu();
                    break;
                }
            }
        }

        private void addToMenu(DXMenuItem menuItem)
        {
            if (customMenu.Items.Count > 0)
            {
                miDuzenle.Items.Add(menuItem);
            }
            else
            {
                RowMenu.Items.Add(menuItem);
            }
        }

        private void addToMenu(DXSubMenuItem subMenu)
        {
            if (customMenu.Items.Count > 0)
            {
                miDuzenle.Items.Add(subMenu);
            }
            else
            {
                RowMenu.Items.Add(subMenu);
            }
        }

        private void AddStandartMenuItems()
        {
            DXMenuItem menuItem;
            menuItem = new DXMenuItem("Copy", new EventHandler(copy), Resources.miCopy);
            menuItem.Shortcut = Shortcut.CtrlC;
            addToMenu(menuItem);

            menuItem = new DXMenuItem("Copy With Headers", new EventHandler(copyWithHeaders));
            addToMenu(menuItem);

            if (!GridUtility.IsReadOnly(GridView) & !IsReadOnly)
            {
                menuItem = new DXMenuItem("Paste", new EventHandler(paste), Resources.miPaste);
                menuItem.Shortcut = Shortcut.CtrlV;
                addToMenu(menuItem);

                menuItem = new DXMenuItem("Delete", new EventHandler(delete), Resources.miDelete);
                menuItem.Shortcut = Shortcut.Del;
                menuItem.BeginGroup = true;
                addToMenu(menuItem);

                menuItem = new DXMenuItem("Delete Row", new EventHandler(deleteRow));
                menuItem.Shortcut = Shortcut.CtrlDel;
                addToMenu(menuItem);
            }

            menuItem = new DXMenuItem("Select All", new EventHandler(selectAll));
            menuItem.Shortcut = Shortcut.CtrlA;
            //menuItem.BeginGroup = true;
            addToMenu(menuItem);

            menuItem = new DXMenuItem("Go To Column", new EventHandler(sutunaGit));
            menuItem.Shortcut = Shortcut.CtrlG;
            menuItem.BeginGroup = true;
            addToMenu(menuItem);

            DXSubMenuItem subMenu = new DXSubMenuItem("Send To");
            addToMenu(subMenu);

            menuItem = new DXMenuItem("Excel", new EventHandler(exportToExcel), Resources.miXls);
            subMenu.Items.Add(menuItem);

            menuItem = new DXMenuItem("PDF", new EventHandler(exportToPDF), Resources.miPDF);
            subMenu.Items.Add(menuItem);

            menuItem = new DXMenuItem("Print", new EventHandler(Yazdir_Click), Resources.miPrint);
            subMenu.Items.Add(menuItem);
        }

        #region Menu Functions


        private void Yazdir_Click(object sender, EventArgs e)
        {
            GridView.ShowPrintPreview();
        }

        private void ColumnAutoWidth_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ColumnAutoWidth = !GridView.OptionsView.ColumnAutoWidth;
        }


        private void AllowCellMerge_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.AllowCellMerge = !GridView.OptionsView.AllowCellMerge;
        }


        private void ShowGroupedColumns_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowGroupedColumns = !GridView.OptionsView.ShowGroupedColumns;
        }


        private void CopyToClipboardWithColumnHeaders_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders = !GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders;
        }


        #endregion

        #region Menu Item Event Handlers
        private void selectAll(object sender, EventArgs e)
        {
            Utility.SelectAll();
        }

        private void sutunaGit(object sender, EventArgs e)
        {
            Utility.SutunaGit();
        }

        private void copy(object sender, EventArgs e)
        {
            Utility.Copy();
        }

        public void copyWithHeaders(object sender, EventArgs e)
        {
            Utility.CopyWithHeaders();
        }

        private void paste(object sender, EventArgs e)
        {
            Utility.PasteFromClipboard();
        }

        private void delete(object sender, EventArgs e)
        {
            Utility.DeleteValue();
        }

        private void deleteRow(object sender, EventArgs e)
        {
            Utility.DeleteRow();
        }

        private void exportToExcel(object sender, EventArgs e)
        {
            Utility.Export("xlsx");
        }

        private void exportToRTF(object sender, EventArgs e)
        {
            Utility.Export("rtf");
        }

        private void exportToPDF(object sender, EventArgs e)
        {
            Utility.Export("pdf");
        }
        #endregion
    }
}